/*******************************************************************************
 * Copyright 2009-2015 Amazon Services. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 *
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 * specific language governing permissions and limitations under the License.
 *******************************************************************************
 * Marketplace Web Service Orders
 * API Version: 2013-09-01
 * Library Version: 2015-04-30
 * Generated: Thu May 28 20:14:49 GMT 2015
 */
package com.amazonservices.mws.orders._2013_09_01.samples;

import com.alibaba.fastjson.JSON;
import com.amazon.items.ListOrderItems;
import com.amazon.items.Orderitems;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrders;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersClient;
import com.amazonservices.mws.orders._2013_09_01.MarketplaceWebServiceOrdersException;
import com.amazonservices.mws.orders._2013_09_01.model.ListOrderItemsRequest;
import com.amazonservices.mws.orders._2013_09_01.model.ListOrderItemsResponse;
import com.amazonservices.mws.orders._2013_09_01.model.ResponseHeaderMetadata;

/** Sample call for ListOrderItems. */
public class ListOrderItemsSample {

	/**
	 * Call the service, log response and exceptions.
	 *
	 * @param client
	 * @param request
	 *
	 * @return The response.
	 */
	public static ListOrderItemsResponse invokeListOrderItems(MarketplaceWebServiceOrders client,
			ListOrderItemsRequest request) {
		try {
			// Call the service.
			ListOrderItemsResponse response = client.listOrderItems(request);
			ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
			// We recommend logging every the request id and timestamp of every
			// call.
			System.out.println("Response:");
			System.out.println("RequestId: " + rhmd.getRequestId());
			System.out.println("Timestamp: " + rhmd.getTimestamp());
			String jsonstr = response.toJSON();
			System.out.println(jsonstr);
			ListOrderItems listOrderItems = JSON.parseObject(jsonstr, ListOrderItems.class);

			Orderitems oditems=listOrderItems.getListorderitemsresult().getOrderitems().get(0);

			return response;
		} catch (MarketplaceWebServiceOrdersException ex) {
			// Exception properties are important for diagnostics.
			System.out.println("Service Exception:");
			ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();
			if (rhmd != null) {
				System.out.println("RequestId: " + rhmd.getRequestId());
				System.out.println("Timestamp: " + rhmd.getTimestamp());
			}
			System.out.println("Message: " + ex.getMessage());
			System.out.println("StatusCode: " + ex.getStatusCode());
			System.out.println("ErrorCode: " + ex.getErrorCode());
			System.out.println("ErrorType: " + ex.getErrorType());
			throw ex;
		}
	}

	/**
	 * Command line entry point.
	 */
	public static void main(String[] args) {

		// Get a client connection.
		// Make sure you've set the variables in
		// MarketplaceWebServiceOrdersSampleConfig.
		MarketplaceWebServiceOrdersClient client = MarketplaceWebServiceOrdersSampleConfig.getClient();

		// Create a request.
		ListOrderItemsRequest request = new ListOrderItemsRequest();
		String sellerId = "A31FK1YOBVUT0K";
		request.setSellerId(sellerId);
		String mwsAuthToken = "example";
		request.setMWSAuthToken(mwsAuthToken);
		String amazonOrderId = "114-6824236-0625863";
		request.setAmazonOrderId(amazonOrderId);

		// Make the call.
		ListOrderItemsSample.invokeListOrderItems(client, request);

	}

}
